<?php

namespace app\models;

use Yii;
use yii\helpers\Url;

/**
 * This is the model class for table "{{%page}}".
 *
 * @property int $id 文档id
 * @property string $page_title 文档标题
 * @property int $created_uid 创建者uid
 * @property string $created_time 创建时间
 * @property int $sort 序号
 * @property string $content 文档内容
 * @property int $cate_id 分类id
 * @property int $project_id 项目id
 * @property int $status 文档状态0正常 1草稿 2删除 
 */
class Page extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%page}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['created_uid','created_time','sort', 'cate_id', 'project_id', 'status'], 'integer'],
            [['content'], 'required'],
            [['content'], 'string'],
            [['page_title'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'page_title' => 'Page Title',
            'created_uid' => 'Created Uid',
            'created_time' => 'Created Time',
            'sort' => 'Sort',
            'content' => 'Content',
            'cate_id' => 'Cate ID',
            'project_id' => 'Project ID',
            'status' => 'Status',
        ];
    }

    public static function getFormatPage($where)
    {
        $pages = Page::find()->where($where)->asArray()->all();
        $returnData = [];
        if (!empty($pages)){
            array_walk($pages,function ($v) use (&$returnData){
                $temp['id'] = $v['id'];
                $temp['text'] = $v['page_title'];
                $temp['type'] = 'page';
                $temp['href'] = Url::toRoute(['docs/page','projectId' => $v['project_id'],'pageId' => $v['id']]);
                $temp['node'] = [];
                $returnData[] = $temp;
            });
        }
        return $returnData;
    }
}
